package com.hitfm.iplay.activity;

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import android.widget.TabWidget;

import com.hitfm.iplay.MusicPlayerApp;
import com.hitfm.iplay.R;
import com.hitfm.iplay.tab.AnimationTabHost;

public class TabHostActivity extends TabActivity implements OnTabChangeListener {
	private GestureDetector gestureDetector;
	private FrameLayout frameLayout;

	private AnimationTabHost mTabHost;
	private TabWidget mTabWidget;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tabhost);

		mTabHost = (AnimationTabHost) findViewById(android.R.id.tabhost);
		mTabWidget = (TabWidget) findViewById(android.R.id.tabs);
		mTabHost.setOnTabChangedListener(this);

		init();

		new View.OnTouchListener() {
			public boolean onTouch(View v, MotionEvent event) {
				if (gestureDetector.onTouchEvent(event)) {
					return true;
				}
				return false;
			}
		};

		frameLayout = mTabHost.getTabContentView();
		
		MusicPlayerApp musicPlayerApp=(MusicPlayerApp)this.getApplication();
		musicPlayerApp.setTabHost(mTabHost);
		Log.i("TabHostActivity", "TabHostActivity====>>>onCreate()===>>>>frameLayout: " + frameLayout.getChildCount());
	}

	private void init() {
		setIndicator(0, new Intent(this, PlayingActivity.class), R.layout.tab_playing);
		setIndicator(1, new Intent(this, MysongActivity.class), R.layout.tab_mysong);
		setIndicator(2, new Intent(this, PlayingActivity.class), R.layout.tab_online);
		setIndicator(3, new Intent(this, PlayingActivity.class), R.layout.tab_menu);

		mTabHost.setOpenAnimation(true);
	}

	private void setIndicator(int tabId, Intent intent, int layoutid) {

		View localView = LayoutInflater.from(this.mTabHost.getContext()).inflate(layoutid, null);

		String str = String.valueOf(tabId);

		TabHost.TabSpec localTabSpec = mTabHost.newTabSpec(str).setIndicator(localView).setContent(intent);
		mTabHost.addTab(localTabSpec);
	}

	@Override
	public void onTabChanged(String tabId) {
		int tabID = Integer.valueOf(tabId);

		for (int i = 0; i < mTabWidget.getChildCount(); i++) {
			if (i == tabID) {
				mTabWidget.getChildAt(Integer.valueOf(i)).setBackgroundResource(R.color.paleturquoise);
			} else {
				mTabWidget.getChildAt(Integer.valueOf(i)).setBackgroundResource(R.color.dodgerblue);
			}
		}
	}

}